package 常用方法.转化.数值和字符串转化;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 姚东名
 * Date: 2024-06-20
 * Time: 11:43
 */
class Student {
    public String name;
    public int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
public class Test {
    public static void main(String[] args) {
        //1.数字转字符串
        String s1 = String.valueOf(1234);
        String s2 = String.valueOf(12.34);
        String s3 = String.valueOf(true);
        String s4 = String.valueOf(new Student("张三", 19));

        System.out.println(s1);
        System.out.println(s2);
        System.out.println(s3);
        System.out.println(s4);

        System.out.println("===========================");

        //2. 字符串转数字
        //注意：Integer、Double等是Java中的包装类，后面会详细介绍
        int data1 = Integer.parseInt("1234");
        double data2 = Double.parseDouble("12.34");

        System.out.println(data1);
        System.out.println(data2);
        System.out.println(data1 + data2);
    }
}

/*
执行结果：
1234
12.34
true
Student{name='张三', age=19}
===========================
1234
12.34
1246.34
 */